package cn.ljy.uem.web.action.backstage.school;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.web.controller.backstage.BaseBackstageController;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.common.model.result.ExcelImportResult;
import cn.ljy.common.model.result.ResponseData;
import cn.ljy.common.utils.file.MyFileUpload;
import cn.ljy.common.utils.table.MyTableUtil;
import cn.ljy.uem.model.SchoolTeacher;
import cn.ljy.uem.model.query.SchoolStudentQuery;
import cn.ljy.uem.model.query.SchoolTeacherQuery;
import cn.ljy.uem.service.SchoolTeacherExcelService;
import cn.ljy.uem.service.SchoolTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
/**
 * 教师模块控制器
 *
 * @author 小锋
 * @date 2022/07/18
 */
@Api(tags = "后台的教师模块")
@RestController
@RequestMapping(value = "/api/backstage/school/schoolTeacher")
public class SchoolTeacherController extends BaseBackstageController<SchoolTeacher, SchoolTeacherQuery,Integer> {

    @Autowired
    private SchoolTeacherService schoolTeacherService;

    @Autowired
    private SchoolTeacherExcelService schoolTeacherExcelService;

    @ApiOperation(value = "查询所有教师")
    @GetMapping("/all")
    @Override
    protected ResponseData query(SchoolTeacherQuery query) {
        return super.query(query);
    }

    @ApiOperation(value = "分页查询教师列表")
    @Override
    public ResponseData query(Integer page, Integer pageSize, SchoolTeacherQuery query){
        return super.query(page,pageSize,query);
    }

    @ApiOperation(value = "读取教师信息")
    @Override
    protected ResponseData get(@PathVariable("id") Integer id) {
        return super.get(id);
    }

    @ApiOperation(value = "批量删除指定的多个教师")
    @Override
    protected ResponseData deletes(@NotEmpty(message = "操作失败：请选择要移除的记录") Integer[] ids) {
        return super.deletes(ids);
    }


    @ApiOperation(value = "添加教师")
    @Override
    protected ResponseData add(@Valid @RequestBody SchoolTeacher schoolTeacher) {
        return super.add(schoolTeacher);
    }

    @ApiOperation(value = "编辑教师")
    @Override
    protected ResponseData edit(@Valid @RequestBody SchoolTeacher schoolTeacher) {
        return super.edit(schoolTeacher);
    }

    /**
     * 上传表格，并导入到项目中
     * @param file 要上传的excel
     * @return
     */
    @ApiOperation(value = "导入账户表excel数据")
    @PostMapping("/excel")
    public ResponseData uploadExcel(MultipartFile file) throws Exception{
        if(file!=null&&!file.isEmpty()){
            String fileName=file.getOriginalFilename();//获取文件名
            String extensionName= FileUtil.extName(fileName);//获取文件扩展名
            //验证表格的格式是否正确
            MyFileUpload.validateExtByDir(extensionName,"table");
            //执行表格导入
            ExcelImportResult result=schoolTeacherExcelService.addUsersFromExcel(file.getInputStream(), UserUtil.getLoginUser());
            //String filePath=uploadFolder+Constant.MATERIAL_EXCEL_UPLOAD_PATH;//获取excel上传后保存的物理路径
            return ResponseData.success(result);
        }else{
            throw new MyWebException("操作失败：请选择上传文件");
        }
    }

    /**
     * 导出表格模板
     */
    @ApiOperation(value = "导出账户模块的excel数据")
    @GetMapping("/excel")
    public void downloadExcel(HttpServletResponse response, SchoolTeacherQuery schoolTeacherQuery){
        ExcelWriter writer=schoolTeacherExcelService.exportExcel(false,schoolTeacherQuery);
        MyTableUtil.downloadExcelByHutool(response,"账户表格",writer);
    }

    /**
     * 导出表格
     */
    @ApiOperation(value = "导出账户的excel模板")
    @GetMapping("/excel/template")
    public void downloadExcelTemplate(HttpServletResponse response){
        ExcelWriter writer=schoolTeacherExcelService.exportExcel(true,null);
        MyTableUtil.downloadExcelByHutool(response,"账户表格模板",writer);
    }

}
